<?php
class ProjectsController extends AppController {

	var $name = 'Projects';
 //   var $scaffold;
    
       
	function index() {
		$this->Project->recursive = 2;
		$this->set('projects', $this->paginate());
 
              
    }

	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid project', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('project', $this->Project->read(null, $id));
        
        $companyProject = $this->Project->read("company_id");
        $this->set('companyProject', $this->Project->Company->read(null, $companyProject['Project']['company_id'])); 
        
        $stateProject = $this->Project->read("state_id");
        $this->set('stateProject', $this->Project->State->read(null, $stateProject['Project']['state_id'])); 
   
	}

	function add() {
		if (!empty($this->data)) {
			$this->Project->create();
           
            //El proyecto se crea en stado: Iniciado
            $this->data['Project']['state_id'] = 5;
            
			if ($this->Project->save($this->data)) {
				$this->Session->setFlash(__('The project has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The project could not be saved. Please, try again.', true));
			}
		}
        
        $companies = $this->Project->Company->find('list');
        $this->set(compact('companies')); 
        
        
        
	}

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid project', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
            
           // $this->Project->Company->id = $this->data ['Project']['Company']["id"];
			
            if ($this->Project->save($this->data)) {
				$this->Session->setFlash(__('The project has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The project could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Project->read(null, $id);
   		}
        
        $companies = $this->Project->Company->find('list');
        $this->set(compact('companies')); 
        
       /* $states = $this->Project->State->find('list');
        $this->set(compact('states'));   */

	}
    


	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for project', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Project->delete($id)) {
			$this->Session->setFlash(__('Project deleted', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('Project was not deleted', true));
		$this->redirect(array('action' => 'index'));
	} 
    
 
}
?>